{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c72d57c3",
   "metadata": {},
   "source": [
    "# IMPORT LIBRARIES"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f861754e",
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "\n",
    "import json\n",
    "import os\n",
    "import networkx as nx\n",
    "import numpy as np\n",
    "\n",
    "import matplotlib as mpl\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.ticker as ticker\n",
    "import matplotlib.colors as mplclr\n",
    "import copy as cp\n",
    "import itertools\n",
    "    \n",
    "    \n",
    "mpl.rcParams['figure.figsize'] = (10,10)\n",
    "mpl.rcParams['xtick.direction'] = 'in'\n",
    "mpl.rcParams['ytick.direction'] = 'in'\n",
    "mpl.rcParams['mathtext.fontset'] = 'cm'\n",
    "mpl.rcParams['mathtext.rm'] = 'serif'\n",
    "\n",
    "print('libraries loaded!')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17d79d83",
   "metadata": {},
   "source": [
    "# Loading common variables"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ba8c1a1e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# working directory\n",
    "WD = os.getcwd()\n",
    "\n",
    "print('Workind Directory is ', WD)\n",
    "\n",
    "\n",
    "# output directory\n",
    "outdirname = WD[:-4]+'results/'\n",
    "\n",
    "print('Output Directory is', outdirname)\n",
    "\n",
    "\n",
    "# RGB custom color definition\n",
    "# (from https://coolors.co/8ecae6-219ebc-023047-ffb703-fb8500)\n",
    "\n",
    "mycustom_colors = {\"mycol1\":\"#8ecae6\",\\\n",
    "                   \"mycol2\":\"#219ebc\",\\\n",
    "                   \"mycol3\":\"#023047\",\\\n",
    "                   \"mycol4\":\"#ffb703\",\\\n",
    "                   \"mycol5\":\"#fb8500\"}\n",
    "\n",
    "# RGB custom color definition\n",
    "# (from https://coolors.co/264653-2a9d8f-e9c46a-f4a261-e76f51)\n",
    "\n",
    "#mycustom_colors = {\"mycol1\":\"#264653\",\\\n",
    "#                   \"mycol2\":\"#2a9d8f\",\\\n",
    "#                   \"mycol3\":\"#e9c46a\",\\\n",
    "#                   \"mycol4\":\"#f4a261\",\\\n",
    "#                   \"mycol5\":\"#e76f51\"}\n",
    "\n",
    "\n",
    "# Definisco i colori per men, women, unknown\n",
    "\n",
    "mycolor_men = mycustom_colors['mycol3']\n",
    "mycolor_women = mycustom_colors['mycol5']\n",
    "mycolor_unkno = mycustom_colors['mycol1']\n",
    "\n",
    "print('Common variables loaded!')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ada25c46",
   "metadata": {},
   "source": [
    "# Functions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2db1c8de",
   "metadata": {},
   "outputs": [],
   "source": [
    "def reading_net(lwd, lfname):\n",
    "    \"\"\"Reading the files of nodes/edges and using them to populate the networks\"\"\"\n",
    "    \n",
    "    ldirinname = lwd[:-4]+\"data/\"\n",
    "    lfnamenodes = lfname+'-graph_nodes.json'\n",
    "    lfnameedges = lfname+'-graph_edges.json'\n",
    "\n",
    "    with open(ldirinname+lfnamenodes) as jfile:\n",
    "        lndata = json.load(jfile)\n",
    "\n",
    "        \n",
    "    with open(ldirinname+lfnameedges) as jfile:\n",
    "        ledata = json.load(jfile)\n",
    "\n",
    "    # creating the graph\n",
    "    print('\\tCreating graph %s ...' %(lfname))\n",
    "    lmyG = nx.Graph()\n",
    "    \n",
    "    for k,v in lndata.items():\n",
    "        lmyG.add_node(int(k), name=v['name'], gender=v['gender'])\n",
    "    \n",
    "    print('\\tThe network has %d nodes' %(lmyG.number_of_nodes()))\n",
    "        \n",
    "    for k,v in ledata.items():\n",
    "        lidfrom = int(v[0])\n",
    "        lidto = int(v[1])\n",
    "        lmyG.add_edge(lidfrom, lidto, weight=v[2]['weight'], etype=v[2]['etype'])\n",
    "    \n",
    "    print('\\tThe network has %d edges' %(lmyG.number_of_edges()))\n",
    "    \n",
    "    \n",
    "    return lmyG\n",
    "    \n",
    "\n",
    "def jaccard_edgesets(lG1, lG2):\n",
    "    \"\"\"Computing the Jaccard score of the edge sets\"\"\"\n",
    "    \n",
    "    leset1 = set()\n",
    "    leset2 = set()\n",
    "    \n",
    "    for e in lG1.edges():\n",
    "        lid1 = e[0]\n",
    "        lid2 = e[1]\n",
    "        \n",
    "        leset1.add((lid1,lid2))\n",
    "        leset1.add((lid2,lid1))\n",
    "        \n",
    "    for e in lG2.edges():\n",
    "        lid1 = e[0]\n",
    "        lid2 = e[1]\n",
    "        \n",
    "        leset2.add((lid1,lid2))\n",
    "        leset2.add((lid2,lid1))\n",
    "    \n",
    "    lintsize = float(len(leset1.intersection(leset2)))\n",
    "    lunisize = float(len(leset1.union(leset2)))\n",
    "    \n",
    "    ljaccard = lintsize/lunisize\n",
    "    \n",
    "    return ljaccard\n",
    "\n",
    "\n",
    "def component_analysis(lmyG):\n",
    "    \"\"\"Analizes the components of the network \"\"\"\n",
    "    \n",
    "    # giant component\n",
    "    lgccG = lmyG.subgraph(max(nx.connected_components(lmyG), key=len)).copy()\n",
    "    lgcc_nrnodes = lgccG.number_of_nodes()\n",
    "    lsize_giant = float(lgcc_nrnodes)/float(lmyG.number_of_nodes())\n",
    "    \n",
    "    \n",
    "    lcompons = [len(c) for c in sorted(nx.connected_components(lmyG), key=len, reverse=True)]\n",
    "    lnrcompons = len(lcompons)\n",
    "        \n",
    "    lnr_isol_nodes = 0\n",
    "    for e in lcompons:\n",
    "        if e == 1:\n",
    "            lnr_isol_nodes += 1\n",
    "            \n",
    "    # return nr components | nr isolated nodes | size giant\n",
    "    return lnrcompons, lnr_isol_nodes, lsize_giant\n",
    "    \n",
    "\n",
    "# Declaring a function to extract the top-n list of authors according\n",
    "# to a given centrality\n",
    "\n",
    "def extract_top(lnrtop, lcentr_dict, lG):\n",
    "    \"\"\"Extracting the list of the top-n male & female authors according to\n",
    "       a given centrality index\n",
    "       \n",
    "       INPUT\n",
    "       \n",
    "       lnrtop --> number of top-n to extract (eg. 100)\n",
    "       lcentr_dict --> dictionary of (node_id, centrality) pairs\n",
    "       lG --> graph (to extract the authors' names)\n",
    "       \n",
    "       \n",
    "       OUTPUT\n",
    "       \n",
    "       lm_top --> list of top-n male authors\n",
    "       lf_top --> list of top-n female authors\n",
    "       \n",
    "       \"\"\"\n",
    "\n",
    "    lm_top = []\n",
    "    lf_top = []\n",
    "    \n",
    "    lsorted_centr = sorted(lcentr_dict.items(), key=lambda kv: kv[1], reverse=True)\n",
    "\n",
    "    #print('Preparing the list of top-%d most central male/female' %(lnrtop))\n",
    "\n",
    "    for elem in lsorted_centr:\n",
    "        lmyid = elem[0]\n",
    "        lmycentr = elem[1]\n",
    "        lmygend = lG.nodes[lmyid]['gender']\n",
    "        lmyname = lG.nodes[lmyid]['name']\n",
    "    \n",
    "        if ((lmygend == 'male') and (len(lm_top) < lnrtop)):\n",
    "            lm_top.append((lmyname,lmycentr))\n",
    "        \n",
    "        if ((lmygend == 'female') and (len(lf_top) < lnrtop)):\n",
    "            lf_top.append((lmyname,lmycentr))\n",
    "        \n",
    "        if ((len(lm_top) == lnrtop) and (len(lf_top) == lnrtop)):\n",
    "            break\n",
    "\n",
    "            \n",
    "    return lm_top, lf_top\n",
    "\n",
    "\n",
    "def swap_edges(lG,e1,e2):\n",
    "    \"\"\"Swaps the stubs of two edges while avoiding self-loops and existing edges\"\"\"\n",
    "    \n",
    "    retval = 0\n",
    "    \n",
    "    e11 = e1[0]\n",
    "    e12 = e1[1]\n",
    "    \n",
    "    e21 = e2[0]\n",
    "    e22 = e2[1]\n",
    "    \n",
    "    \n",
    "    # Swap (e11,e12),(e21,e22) --> (e11,e22),(e12,e21)\n",
    "    \n",
    "    if ((e11 != e22) and (e12 != e21)):\n",
    "        if (not lG.has_edge(e11,e22)) and (not lG.has_edge(e12,e21)):\n",
    "            lG.remove_edge(e11,e12)\n",
    "            lG.remove_edge(e21,e22)\n",
    "            \n",
    "            lG.add_edges_from([(e11,e22),(e12,e21)])\n",
    "            \n",
    "            retval = 1\n",
    "            return retval\n",
    "    \n",
    "    # Swap (e11,e12),(e21,e22) --> (e11,e21),(e12,e22)\n",
    "    \n",
    "    if ((e11 != e21) and (e12 != e22)):\n",
    "        if (not lG.has_edge(e11,e21)) and (not lG.has_edge(e12,e22)):\n",
    "            lG.remove_edge(e11,e12)\n",
    "            lG.remove_edge(e21,e22)\n",
    "            \n",
    "            lG.add_edges_from([(e11,e21),(e12,e22)])\n",
    "            \n",
    "            retval = 1\n",
    "            return retval\n",
    "\n",
    "        \n",
    "    # in caso lo scambio non sia possibile, restituisco 0\n",
    "    return retval\n",
    "    \n",
    "\n",
    "print('Functions loaded!')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a7c2e8d5",
   "metadata": {},
   "source": [
    "# Reading Networks"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "15deadd2",
   "metadata": {},
   "outputs": [],
   "source": [
    "print('Reading the networks')\n",
    "\n",
    "# collective bodies\n",
    "cbG = reading_net(WD, 'cb')\n",
    "\n",
    "# events\n",
    "evG = reading_net(WD, 'ev')\n",
    "\n",
    "# publication\n",
    "pbG = reading_net(WD, 'pub')\n",
    "\n",
    "# correspondence\n",
    "coG = reading_net(WD, 'cor')\n",
    "\n",
    "# personal relationships\n",
    "reG = reading_net(WD, 'rel')\n",
    "\n",
    "# defining some common variables\n",
    "\n",
    "graph_list = [cbG, evG, pbG, coG, reG]\n",
    "label_list = ['CB', 'EV', 'PUB', 'COR', 'REL']\n",
    "nr_graphs = len(graph_list)\n",
    "\n",
    "\n",
    "print('End of reading')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dc8b9ffe",
   "metadata": {},
   "source": [
    "## Computing nr of men, women, other"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "adeda97d",
   "metadata": {},
   "outputs": [],
   "source": [
    "nrmen = 0\n",
    "nrwomen = 0\n",
    "nrunkow = 0\n",
    "\n",
    "for n in cbG.nodes(data=True):\n",
    "    gen = n[1]['gender']\n",
    "    \n",
    "    if gen == 'male':\n",
    "        nrmen += 1\n",
    "    elif gen == 'female':\n",
    "        nrwomen += 1\n",
    "    else:\n",
    "        nrunkow += 1\n",
    "        \n",
    "print('Of %d nodes, we have %d men, %d women and %d unknown (sum = %d)'\\\n",
    "      %(cbG.number_of_nodes(), nrmen, nrwomen, nrunkow, sum([nrmen,nrwomen,nrunkow])))\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "62de2bc5",
   "metadata": {},
   "source": [
    "## Computing edge overlap (via Jaccard score)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3b57828c",
   "metadata": {},
   "outputs": [],
   "source": [
    "jacmatrix = np.ones((nr_graphs,nr_graphs))\n",
    "\n",
    "print('Computing the edge overlap ...')\n",
    "\n",
    "for i in range(nr_graphs-1):\n",
    "    myG1 = graph_list[i]\n",
    "    myl1 = label_list[i]\n",
    "    for j in range(i+1, nr_graphs):\n",
    "        myG2 = graph_list[j]\n",
    "        myl2 = label_list[j]\n",
    "        \n",
    "        myjac = jaccard_edgesets(myG1, myG2)\n",
    "        \n",
    "        jacmatrix[i][j] = myjac\n",
    "        jacmatrix[j][i] = myjac\n",
    "        \n",
    "        print('\\tThe Jaccard between %s and %s is %.4f' %(myl1, myl2, myjac))\n",
    "\n",
    "# saving results on file\n",
    "fnameout = 'graphs-edge-jaccard_data.dat'\n",
    "print('Saving file %s ...\\n' %(fnameout))\n",
    "\n",
    "np.savetxt(outdirname+fnameout, jacmatrix, fmt='%.4f')\n",
    "    \n",
    "print('done')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3be79878",
   "metadata": {},
   "outputs": [],
   "source": [
    "jacmatrix"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f0d8d64b",
   "metadata": {},
   "source": [
    "## Computing components in the network"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5fa20b01",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(nr_graphs):\n",
    "    \n",
    "    myG = graph_list[i]\n",
    "    mylabel = label_list[i]\n",
    "    \n",
    "    nrcomps, nr_isol_nod, size_giant = component_analysis(myG)\n",
    "    \n",
    "    print('For graph %s we have:' %(mylabel))\n",
    "    print('\\t%d  %d  %d  %.4f\\n' %(myG.number_of_nodes(), nrcomps, nr_isol_nod, size_giant))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1f5f901e",
   "metadata": {},
   "source": [
    "## Analysis of the degrees"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e992ec2b",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(nr_graphs):\n",
    "    \n",
    "    myG = graph_list[i]\n",
    "    mylabel = label_list[i]\n",
    "    \n",
    "    degs = [e[1] for e in myG.degree()]\n",
    "    mindeg = min(degs)\n",
    "    maxdeg = max(degs)\n",
    "    \n",
    "    print('For network %s mindeg = %d  and  maxdeg = %d' %(mylabel, mindeg, maxdeg))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8bc5f176",
   "metadata": {},
   "source": [
    "## k-core Decomposition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13f324bb",
   "metadata": {},
   "outputs": [],
   "source": [
    "nrtop = 50\n",
    "\n",
    "print('Performing the k-core decomposition ...')\n",
    "\n",
    "for i in range(nr_graphs):\n",
    "    \n",
    "    myG = graph_list[i]\n",
    "    mylabel = label_list[i]\n",
    "\n",
    "    print(\"\\tIterating over graph %s\" %(mylabel))\n",
    "    \n",
    "    myG.remove_edges_from(nx.selfloop_edges(myG))\n",
    "    \n",
    "    nx_coreness = nx.core_number(myG)\n",
    "\n",
    "    # extracting the n-top inner k-core\n",
    "\n",
    "    m_kc_ntop, f_kc_ntop = extract_top(nrtop, nx_coreness, myG)\n",
    "\n",
    "    # extracting the degeneracy\n",
    "\n",
    "    degen = max(nx_coreness.values())\n",
    "\n",
    "    print('\\t\\tThe degeneracy of the network is %d \\n'%(degen))\n",
    "    \n",
    "    # computing the \"density of women\" per k-shell \n",
    "    # the columns 1 to 3 are the \"raw\" densities whereas columns 4 to 6\n",
    "    # are the cumulative values\n",
    "\n",
    "    ks_gender_density = np.zeros((degen+1,7))\n",
    "\n",
    "    for k,v in nx_coreness.items():\n",
    "        ks = v\n",
    "        gend = myG.nodes[k]['gender']\n",
    "\n",
    "        # filling data structure\n",
    "        ks_gender_density[ks][0] = ks\n",
    "\n",
    "        # updating the counters\n",
    "        if gend == 'male':\n",
    "            ks_gender_density[ks][1] += 1.\n",
    "        elif gend == 'female':\n",
    "            ks_gender_density[ks][2] += 1.\n",
    "        else:\n",
    "            ks_gender_density[ks][3] += 1.\n",
    "\n",
    "    # populating the cumulative part\n",
    "    for i in range(len(ks_gender_density)):\n",
    "        ks_gender_density[i][0] = i\n",
    "        ks_gender_density[i][4] = sum(ks_gender_density[i:,1])\n",
    "        ks_gender_density[i][5] = sum(ks_gender_density[i:,2])\n",
    "        ks_gender_density[i][6] = sum(ks_gender_density[i:,3])\n",
    "\n",
    "\n",
    "    # normalization\n",
    "\n",
    "    for i in range(len(ks_gender_density)):\n",
    "        norm = sum(ks_gender_density[i][1:4])\n",
    "\n",
    "        nr_m = ks_gender_density[i][4]\n",
    "        nr_f = ks_gender_density[i][5]\n",
    "        nr_o = ks_gender_density[i][6]\n",
    "\n",
    "        norm2 = nr_m + nr_f + nr_o\n",
    "\n",
    "        for j in range(4,7):\n",
    "            ks_gender_density[i][j] /= norm2\n",
    "\n",
    "        if norm > 0:\n",
    "            for j in range(1,4):\n",
    "                ks_gender_density[i][j] /= norm\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "    # saving on file\n",
    "\n",
    "    dummy = list()\n",
    "    count = 0\n",
    "\n",
    "    for i in range(len(ks_gender_density)):\n",
    "        mysum = sum(ks_gender_density[i,1:4])\n",
    "\n",
    "        if mysum > 0.:\n",
    "            dummy.append([count, int(ks_gender_density[i][0]),\\\n",
    "                          ks_gender_density[i][1],\\\n",
    "                          ks_gender_density[i][2],\\\n",
    "                          ks_gender_density[i][3]])\n",
    "            count += 1\n",
    "\n",
    "    fnameout = 'k_core-gender-net_'+mylabel+'-data.dat'\n",
    "    myfmt = '%d %d %.8e %.8e %.8e'\n",
    "    print('Saving file %s ...\\n' %(fnameout))\n",
    "\n",
    "    np.savetxt(outdirname+fnameout, np.asarray(dummy), fmt=myfmt)\n",
    "    \n",
    "    print('\\n\\n')\n",
    "\n",
    "print('End of k-core analysis')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b0ae050e",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(nr_graphs):\n",
    "    \n",
    "    myG = graph_list[i]\n",
    "    mylabel = label_list[i]\n",
    "\n",
    "    print(\"\\tIterating over graph %s\" %(mylabel))\n",
    "\n",
    "\n",
    "    nx_coreness = nx.core_number(myG)\n",
    "    genders = nx.get_node_attributes(cbG, \"gender\")\n",
    "    names = nx.get_node_attributes(cbG, \"name\")\n",
    "\n",
    "    mylist = []\n",
    "\n",
    "    for k,v in nx_coreness.items():\n",
    "        name = names[k]\n",
    "        gend = genders[k]\n",
    "        if gend == 'female':\n",
    "            #print(k, name, gend, v)\n",
    "            mylist.append((name,v))\n",
    "\n",
    "    mylist.sort(key=lambda tup: tup[1], reverse=True)\n",
    "\n",
    "    for e in mylist:\n",
    "        print('\\t\\t',e[0],' | ', e[1])\n",
    "       \n",
    "    print('\\n\\n')\n",
    "    \n",
    "print('End of lists!')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0c9379b0",
   "metadata": {},
   "source": [
    "# Creation of aleatoric counterpart"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1b3f6b3f",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# COMMON VARIABLES\n",
    "\n",
    "nr_replicas = 50\n",
    "nr_swaps = 10000\n",
    "\n",
    "#### ITERATING OVER GRAPH TYPES #####\n",
    "\n",
    "for i in range(nr_graphs):\n",
    "    \n",
    "    myG = graph_list[i]\n",
    "    mylabel = label_list[i]\n",
    "\n",
    "    print(\"\\tIterating over graph %s\" %(mylabel))\n",
    "    \n",
    "    myG.remove_edges_from(nx.selfloop_edges(myG))\n",
    "    \n",
    "    maxdeg = max([d for k, d in myG.degree()])\n",
    "\n",
    "    ks_gender_density_rand = np.zeros((maxdeg+1,4))\n",
    "\n",
    "    for i in range(maxdeg+1):\n",
    "        ks_gender_density_rand[i][0] = i\n",
    "\n",
    "\n",
    "    for irep in range(nr_replicas):\n",
    "        print('\\t\\tIteration nr. ', irep+1)\n",
    "        myGr = myG.copy()\n",
    "\n",
    "        iswap = 0\n",
    "\n",
    "        while iswap < nr_swaps:\n",
    "            elist = [e for e in myGr.edges()]\n",
    "            nredges = len(elist)\n",
    "            selects = np.random.choice(nredges,2)\n",
    "            edgs = [elist[i] for i in selects]\n",
    "\n",
    "\n",
    "            iswap += swap_edges(myGr,edgs[0],edgs[1])\n",
    "\n",
    "            if ((iswap % 2500) == 0):\n",
    "                print('\\t\\t\\tswap = ', iswap)\n",
    "\n",
    "\n",
    "        # extraction of the k-core\n",
    "        nx_coreness = nx.core_number(myGr)\n",
    "\n",
    "        for k,v in nx_coreness.items():\n",
    "            ks = v\n",
    "            gend = myG.nodes[k]['gender']\n",
    "\n",
    "\n",
    "            # updating the counters\n",
    "            if gend == 'male':\n",
    "                ks_gender_density_rand[ks][1] += 1.\n",
    "            elif gend == 'female':\n",
    "                ks_gender_density_rand[ks][2] += 1.\n",
    "            else:\n",
    "                ks_gender_density_rand[ks][3] += 1.\n",
    "\n",
    "\n",
    "    # normalization\n",
    "\n",
    "    for i in range(maxdeg+1):\n",
    "        norm = sum(ks_gender_density_rand[i][1:4])\n",
    "\n",
    "        if norm > 0:\n",
    "            for j in range(1,4):\n",
    "                ks_gender_density_rand[i][j] /= norm\n",
    "\n",
    "\n",
    "    # saving on file\n",
    "\n",
    "    dummy = list()\n",
    "    count = 0\n",
    "\n",
    "    for i in range(len(ks_gender_density_rand)):\n",
    "        mysum = sum(ks_gender_density_rand[i,1:4])\n",
    "\n",
    "        if mysum > 0.:\n",
    "            dummy.append([count, int(ks_gender_density_rand[i][0]),\\\n",
    "                          ks_gender_density_rand[i][1],\\\n",
    "                          ks_gender_density_rand[i][2],\\\n",
    "                          ks_gender_density_rand[i][3]])\n",
    "            count += 1\n",
    "\n",
    "    fnameout = 'k_core-gender-net_'+mylabel+'-rand_nrep-'+repr(nr_replicas)+'-data.dat'\n",
    "    myfmt = '%d %d %.8e %.8e %.8e'\n",
    "    print('Saving file %s ...\\n' %(fnameout))\n",
    "\n",
    "    np.savetxt(outdirname+fnameout, np.asarray(dummy), fmt=myfmt)\n",
    "\n",
    "            \n",
    "print('End of k-core analysis')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3f888cae",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
